package bank
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import global.GameState;
	import global.Resource;
	
	import utils.DisplayTool;
	import utils.TextTool;
	
	import view.components.IconButton;
	import view.components.Input;
	import view.components.LoadingBar;
	import view.components.RemoteHtmlImg;
	import view.components.SimpleLoading;
	
	public class BankLand extends Sprite
	{
		private var chests:Array;
		private var myHead:RemoteHtmlImg;
		private var nameInput:Input;
		private var levelInput:TextField;
		private var expBar:LoadingBar;
		private var moneyInput:Input;
		private var simpleLoading:SimpleLoading;
		private var bankLandControl:BankLandControl = BankLandControl.getInstance();
		
		private var contentLayer:Sprite;
		
		
		private var bankLeft:BankLeft;
		internal var homeBut:IconButton;
		
		public function BankLand()
		{
			initStage();
			
			initControl();
			
			initLine();
			
			initHead();
			initText();
			initExpBar();
			
			
			initSimpleLoading()
		
			
			initChests();
			
			initBankLeft();
			initHomeBut();
		}
		
		private function initHomeBut():void
		{
			homeBut = new IconButton(393,-30,0,onHomeBut,"panel_mine","回到自己银行");
			homeBut.visible = false;
			addChild(homeBut);
		}
		
		private function onHomeBut(event:MouseEvent):void
		{
			bankLandControl.loadBankInfo(GameState.snsId);
			homeBut.visible = false;
		}
		
		private function initBankLeft():void
		{
			bankLeft = new BankLeft();
			bankLeft.x = 530;
			bankLandControl.bankLeft = bankLeft;
			bankLandControl.initSearchListener();
			addChild(bankLeft);
		}
		
		private function testHint():void
		{
			var bar:LoadingBar = new LoadingBar();
			addChild(bar);
			bar.percent = .5;
		}
		
		public function init(playerId:String):void
		{
			bankLandControl.loadBankInfo(playerId);
			bankLandControl.getLevelList();
			bankLandControl.getFriendList();
		}
		
		private function initControl():void
		{
			contentLayer = new Sprite();
			addChild(contentLayer);
			bankLandControl.bankLand = this;
		}
		
		private function initSimpleLoading():void
		{
			simpleLoading = new SimpleLoading();
		}
		
		public function showLoading():void
		{
			//simpleLoading.x = (this.width - simpleLoading.width) * .5 + simpleLoading.width * .5;
			//simpleLoading.y = (this.height - simpleLoading.height) * .5 + simpleLoading.height* .5;
			simpleLoading.x = 250;
			simpleLoading.y = 200;
			
			simpleLoading.start();
			addChild(simpleLoading);
			
			contentLayer.visible = false;
		}
		
		public function hideLoading():void
		{
			simpleLoading.stop();
			DisplayTool.removeChild(this,simpleLoading);
			contentLayer.visible = true;
		}
		
		
		
		
		
		private function initLine():void
		{
			with(this.graphics)
			{
				lineStyle(1,0x999999);
				
				beginFill(0x222222);
				drawRoundRect(10,-10,480,340,15,15);
				endFill();
				
				
				moveTo(10,70);
				lineTo(490,70);
				
				lineStyle(2,0x444444);
				moveTo(510,-10);
				lineTo(510,330);
			}
		}
		
		private function initExpBar():void
		{
			expBar = new LoadingBar("face_expFace","face_expBg");
			expBar.x = 260;
			expBar.y = 23;
			contentLayer.addChild(expBar);
		}
		
		private function initText():void
		{
			nameInput = new Input(106,19);
			nameInput.x = 120;
			nameInput.y = 20;
			nameInput.text = "郭皇的家";
			nameInput.selectable = false;
			contentLayer.addChild(nameInput);
			
			levelInput = TextTool.getTextField(40,20,12,0xFFFFFF);;
			levelInput.x = 231;
			levelInput.y = 20;
			levelInput.text = "4级";
			levelInput.selectable = false;
			contentLayer.addChild(levelInput);
			
			moneyInput = new Input(85,19,0xFFFF00,true,"right");
			moneyInput.x = 365;
			moneyInput.y = 20;
			moneyInput.text = "100000";
			moneyInput.selectable = false;
			contentLayer.addChild(moneyInput);
			
			contentLayer.addChild(Resource.getImgXY(350,20,"face_gold"));
		}
		
		private function initHead():void
		{
			myHead = new RemoteHtmlImg(50,50);
			myHead.x = 45;
			contentLayer.addChild(myHead);
		}
		
		private function initChests():void
		{
			chests = [];
			var chest:Chest;
			for ( var i:int=0; i<8; i++)
			{
				chest = new Chest(Math.floor(i / 6),i,bankLandControl);
				chest.state = 0;
				chest.x = (i % 4) * 110 + 48;
				chest.y = Math.floor(i / 4) * 120 + 120;
				
				if (i >= 6)
				{
					chest.y = 219;	
				}
				
				chest.state = i % 5;
				contentLayer.addChild(chest);
				chests.push(chest);
			}
		}
		
		private function initStage():void
		{
			if (stage)
			{
				stage.scaleMode = "noScale"
			}
		}
		
		public function updateChestState(chestId:int,state:int):void
		{
			var chest:Chest = chests[chestId];
			if (chest)
			{
				chest.state = state;
			}
			
		}
		
		public function updateChestHint(chestId:int,content:String):void
		{
			var chest:Chest = chests[chestId];
			if (chest)
			{
				chest.updateHint(content);
			}
		}
		
		public function clear(isClearHint:Boolean):void
		{
			var chest:Chest;
			for each (chest in chests)
			{
				chest.clear(isClearHint);
			}
		}
		
		public function updateChestAnimate(chestId:int):void
		{
			var chest:Chest = chests[chestId];
			if (chest)
			{
				chest.playTakeAnimate();	
			}
		}
		
		public function updateChestMoneyChange(chestId:int,value:int):void
		{
			var chest:Chest = chests[chestId];
			if (chest)
			{
				chest.updateMoneyChange(value);
			}
		}
	
		public function update(xml:XML):void
		{
			bankLeft.prevBut.mouseEnabled = true;
			bankLeft.nextBut.mouseEnabled = true;
			
			nameInput.text = xml.@name;
			myHead.loadImg(xml.@image);
			levelInput.text = xml.@level + "级";
			
			if (int(xml.@nextExp) == 0)
			{
				expBar.percent = 0;
			}
			else
			{
				expBar.percent = int(xml.@exp)/int(xml.@nextExp);
			}
			moneyInput.text = xml.@cash;
			
			var boxXml:XML;
			var chest:Chest;
			for each (boxXml in xml.children())
			{
				
				chest = chests[int(boxXml.@type)];
				chest.chestData = boxXml;
				
				chest.level = String(boxXml.@level);
				chest.state = int(boxXml.@state);
				chest.leftTime = int(boxXml.@leftTime);
					
				/*switch (chest.state)
				{
					case ChestTool.CHEST_STATE_INVALID:
					case ChestTool.CHEST_STATE_ACTIVATE:
					case ChestTool.CHEST_STATE_EMPTY:
					
						chest.info = "<font color='#EB4600' size='14'><b>" +  boxXml.@name + "</b></font>" +
							"\n" + 
							"<font color='#333333'>" + boxXml.@mouseOnDesc + "</font>";
						break;
					case ChestTool.CHEST_STATE_STORE:
						
						chest.info = "<font color='#EB4600' size='14'><b>" +  boxXml.@name + "</b></font>" +
							"\n" +
							"<font size = '12' color='#FF0000'>距离收获还剩：" + boxXml.@leftTime + "</font>\n" + 
							"<font color='#333333'>" + boxXml.@mouseOnDesc + "</font>";
						
						break;
					case ChestTool.CHEST_STATE_HAVEST:
						if (bankLandControl.isSelfBank())
						{
							chest.info = "<font color='#EB4600' size='14'><b>" +  boxXml.@name + "</b></font>" +
								"\n" +
								"<font size = '12' color='#FF0000'>已到期，点击收金币了！</font>\n" + 
								"<font color='#333333'>" + boxXml.@mouseOnDesc + "</font>";
						}
						else
						{
							if (String(boxXml.@leftTime) == "")
							{
								chest.info = "<font color='#EB4600' size='14'><b>" +  boxXml.@name + "</b></font>" +
									"\n" +
									"<font size = '12' color='#FF0000'>到期了，乘主人不在快取吧!</font>" + 
									"<font color='#333333'>\n" + boxXml.@mouseOnDesc + "</font>";
							}
							else
							{
								chest.info = "<font color='#EB4600' size='14'><b>" +  boxXml.@name + "</b></font>" +
									"\n" +
									"<font size = '12' color='#FF0000'>距离下次可取还剩：" + boxXml.@leftTime + "</font>\n" + 
									"<font color='#333333'>" + boxXml.@mouseOnDesc + "</font>";
						
							}
						}
						break;
						
				}*/
				
				
				
				if ( int(boxXml.@confirm) == 0)
				{
					
				}
			}
			
		}
	}
}